const expree=require('express');
const router=expree.Router();
const db=require('../utils/db');

router.get('/type',(req,res)=>{
	const searchType='SELECT * FROM type;';
	db.query(searchType,(err,data)=>{
		if(err){
			res.send({msg:err});
		}else{
			res.send({msg:data});
		}
	})
})

router.get('/menu',(req,res)=>{
	console.log(req);
	const searchType=`SELECT * FROM menu;`;
	db.query(searchType,(err,data)=>{
		if(err){
			res.send({msg:err});
		}else{
			res.send({msg:data});
		}
	})
})

router.post('/recharge',(req,res)=>{
	console.log(req);
	const searchType=`INSERT into recharge (content,status,time,UserID) SELECT '${req.body.content}','${req.body.status}','${req.body.time}','${req.body.userid}' FROM userinfo;`;
	const updataType=`UPDATE userinfo SET balance='${req.body.balance}' WHERE id='${req.body.userid}';`;
	db.query(searchType,(err,data)=>{
			if(err){
				res.send({msg:err});
			}else{
				// res.send({msg:data});
				db.query(updataType,(err2,data2)=>{
					if(err2){
						res.send({msg:err2})
					}else{
						res.send({data:data,data2:data2})
					}
				})
			}
		})
})

router.post('/getRecharge',(req,res)=>{
	console.log(req);
	const searchType=`SELECT r.content,r.status,r.time,u.balance FROM recharge AS r INNER JOIN userinfo AS u ON r.UserID = u.id WHERE r.UserID='${req.body.userid}';`;
	db.query(searchType,(err,data)=>{
		if(err){
			res.send({msg:err});
		}else{
			res.send({msg:data});
		}
	})
})


module.exports=router;